// subpages0/exchangeVip/exchangeVip.js
var app = getApp();
Page({
  data: {

  },
  onLoad: function (e) {
    var that = this;
    if (!app.globalData.plumSession) {
      console.log("暂未获取到session");
      app.wechatSq(that);
    } else {
      that.requestCardinfo();
    }
  },
  onShow: function () {

  },
  requestCardinfo: function () {
    var that = this;
    var data = {};
    data.map = 'applet_community_exchange_card';
    //发起请求，获取列表列表
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var cardInfo = res.data.data;
          var cardList = cardInfo.cardList;
          if (cardList && cardList.length>0){
            that.setData({
              curCardinfo: cardList[0]
            })
          }
          that.setData({
            points: cardInfo.points,
            cardList: cardList
          })
          
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideToast();
      }
    });
  },
  toggleCard:function(e){
    var that = this;
    var curCardinfo = e.currentTarget.dataset.card;
    that.setData({
      curCardinfo: curCardinfo
    })
  },
  confirmExchange:function(){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认兑换吗？',
      success(res) {
        if (res.confirm) {
          that.requestExchange();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  requestExchange: function () {
    var that = this;
    app.getSubId(that, 'applet_buy_member_card', 'toRequestExchange');
  },
  toRequestExchange: function () {
    var that = this;
    var curCardinfo = that.data.curCardinfo;
    var data = {
      map : 'applet_buy_member_card',
      cardid : curCardinfo.id,
      payType : 3
    };
    console.log(data);
    //发起请求，获取列表列表
    wx.showLoading({
      title: '兑换中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          wx.showModal({
            title: '提示',
            content: '兑换成功',
            showCancel:false,
            success(res) {
              if (res.confirm) {
                that.requestCardinfo();
                wx.navigateTo({
                  url: '/subpages/buyMenberCard/buyMenberCard'
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
})